#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int a[N], vis[N];
vector<int> b;
void solve() {
  cin >> n;
  b.clear();
  rep(i, 1, n) vis[i] = 0;
  rep(i, 1, n) {
    cin >> a[i];
    vis[a[i]] = 1;
  }
  rep(i, 1, n) if (!vis[i]) b.push_back(i);
  if (b.size() == 1 && a[b[0]] == 0) a[b[0]] = b[0];
  int l = 1, h = n;
  while (l <= h && a[l] == l) l++;
  while (l <= h && a[h] == h) h--;
  cout << h + 1 - l << endl;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}